package com.example.himalayaproject.find.broadcast.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import com.example.himalayaproject.R;
import com.example.himalayaproject.find.broadcast.activity.CommonActivity;
import com.example.himalayaproject.find.broadcast.activity.GridViewActivity;
import com.example.himalayaproject.find.broadcast.activity.ProvinceActivity;
import com.example.himalayaproject.find.broadcast.activity.TopMoreActivity;
import com.example.himalayaproject.find.broadcast.adapter.BroadcastAdapter;
import com.example.himalayaproject.find.broadcast.adapter.GridAdapter;
import com.example.himalayaproject.find.broadcast.adapter.TopAdapter;
import com.example.himalayaproject.find.broadcast.bean.Categories;
import com.example.himalayaproject.find.broadcast.bean.Data;
import com.example.himalayaproject.find.broadcast.bean.TopRadios;
import com.example.himalayaproject.find.broadcast.callback.BroadcastHttpService;
import com.example.himalayaproject.find.broadcast.view.BroadcastGridView;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * A simple {@link Fragment} subclass.
 */
public class BroadcastFragment extends Fragment implements Callback<Data>,GridAdapter.OnImageViewClickListener, RadioGroup.OnCheckedChangeListener, CompoundButton.OnCheckedChangeListener {


    private BroadcastAdapter adapter;
    private BroadcastHttpService service;
    private TopAdapter topAdapter;
    private TextView textView;
    private BroadcastGridView gridView;
    private GridAdapter gridAdapter;
    private List<Categories> categories;
    private List<Categories> data;
    private List<Categories> current;
    private List<TopRadios> tops;
    private ScrollView scroll;
    private RadioGroup group;
    private LinearLayout linearLayout;
    private Intent intent;
    private Bundle bundle;
    private RadioButton localButton;
    private RadioButton countryButton;
    private RadioButton internetButton;
    private RadioButton provinceButton;
    private RadioButton topMoreButton;
    private RadioButton localMoreButton;
    private String local;

    public BroadcastFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_broadcast, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        service = new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(BroadcastHttpService.class);
        service.getData().enqueue(this);

        scroll = ((ScrollView) view.findViewById(R.id.bct_scrollView));

        linearLayout = ((LinearLayout) view.findViewById(R.id.bct_ll));
//        scroll.requestChildFocus(linearLayout,null);


        initRadioGroup(view);
        initGridView(view);
        initLocalRadios(view);
        initTopRadios(view);
    }

    private void initRadioGroup(View view) {
        group = ((RadioGroup) view.findViewById(R.id.bct_group));
        localButton = ((RadioButton) view.findViewById(R.id.bct_radio_local));
        countryButton = ((RadioButton) view.findViewById(R.id.bct_radio_country));
        provinceButton = ((RadioButton) view.findViewById(R.id.bct_radio_province));
        internetButton = ((RadioButton) view.findViewById(R.id.bct_radio_internet));
//                scroll.requestChildFocus(group,null);
        group.setOnCheckedChangeListener(this);
    }

    private void initGridView(View view) {
        categories = new ArrayList<>();
        data = new ArrayList<>();
        tops = new ArrayList<>();
        gridView = (BroadcastGridView) view.findViewById(R.id.bct_gridView);
    }

    private void initTopRadios(View view) {
        RecyclerView recyclerTop = (RecyclerView) view.findViewById(R.id.topRadios_recycler);
        topAdapter = new TopAdapter(getContext());
        recyclerTop.setAdapter(topAdapter);
        textView = ((TextView) view.findViewById(R.id.bct_location));
    }

    private void initLocalRadios(View view) {
        RecyclerView recycler = (RecyclerView) view.findViewById(R.id.radios_recycler);
        topMoreButton = ((RadioButton) view.findViewById(R.id.radios_top_more));
        localMoreButton = ((RadioButton) view.findViewById(R.id.radios_more));
        topMoreButton.setOnCheckedChangeListener(this);
        localMoreButton.setOnCheckedChangeListener(this);
        adapter = new BroadcastAdapter(getContext());
        recycler.setAdapter(adapter);
    }

    @Override
    public void onResponse(Call<Data> call, Response<Data> response) {
        adapter.addAll(response.body().getData().getLocalRadios());
        local = response.body().getData().getLocation();
        textView.setText(response.body().getData().getLocation());
        for (int i = 0; i < 3; i++) {
            tops.add(response.body().getData().getTopRadios().get(i));
        }
        topAdapter.addAll(tops);

        data = response.body().getData().getCategories();
        for (int i = 0; i < 8; i++) {
            categories.add(i,data.get(i));
        }
        gridAdapter = new GridAdapter(getContext(),categories,data,this);
        gridView.setAdapter(gridAdapter);

        Log.d("1620",response.body().getData().getCategories().get(0).getName());

    }

    @Override
    public void onFailure(Call<Data> call, Throwable t) {
        t.printStackTrace();
    }

    @Override
    public void onImageViewClick(List<Categories> list) {
        current = new ArrayList<>();
        current.addAll(list);
        gridAdapter = new GridAdapter(getContext(),current,data,this);
        gridView.setAdapter(gridAdapter);
    }



    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.bct_radio_local:
                localButton.setChecked(false);
                intent = new Intent(getActivity(), CommonActivity.class);
                intent.putExtra("type","本地台");
                intent.putExtra("TAG",1);
                startActivity(intent);
                break;
            case R.id.bct_radio_country:
                countryButton.setChecked(false);
                intent = new Intent(getActivity(), CommonActivity.class);
                intent.putExtra("type","国家台");
                intent.putExtra("TAG",2);
                startActivity(intent);
                break;
            case R.id.bct_radio_province:
                provinceButton.setChecked(false);
                intent = new Intent(getActivity(), ProvinceActivity.class);
                intent.putExtra("type","省市台");
                intent.putExtra("TAG",3);
                startActivity(intent);
                break;
            case R.id.bct_radio_internet:
                internetButton.setChecked(false);
                intent = new Intent(getActivity(), CommonActivity.class);
                intent.putExtra("type","网络台");
                intent.putExtra("TAG",4);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.radios_top_more:
                topMoreButton.setChecked(false);
                intent = new Intent(getActivity(), TopMoreActivity.class);
                intent.putExtra("type","电台排行榜");
                intent.putExtra("TAG",5);
                startActivity(intent);
                break;
            case R.id.radios_more:
                localMoreButton.setChecked(false);
                intent = new Intent(getActivity(), GridViewActivity.class);
                intent.putExtra("type",local);
                startActivity(intent);
                break;
        }
    }
}
